<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/** Customer or user class
 *
 * This is customer class to generate the main page for login and registration
 *
 * @author  Zoms
 * @version 39
 * @copyright copyright (c) 2011, Boston Univeristy Under BSD
 * @todo add function to retrive password
 * @todo add function to update profile
 */
class Contact extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    function __construct() {
        parent::__construct();
    }

    function index() {
    
        //calling the main function
        $this->main();
    }

    /** Main function
     *
     *  This function is the main function in this class and will wait for users input from yogette_view website   
     */
    function main() {
        // Load the library
        $this->load->library('googlemaps');
        // Initialize our map. Here you can also pass in additional parameters for customising the map (see below)
         
        $config['geocodeCaching'] = TRUE;
        $config['adsenseFormat'] = 'MEDIUM_RECTANGLE';
        //$config['center'] = 'auto';
        $config['cluster'] = FALSE;
        $config['clusterGridSize'] = '60';
        $config['directions'] = FALSE;
        $config['draggable'] = FALSE;
        $config['https'] = TRUE;
       // $config['adsenseFormat'] = 'MEDIUM_RECTANGLE';
       // $config['adsenseFormat'] = 'MEDIUM_RECTANGLE';
       
        $config['zoom'] = 'auto';

        $this->googlemaps->initialize($config);
        // Create the map. This will return the Javascript to be included in our pages <head></head> section and the HTML code to be
        // placed where we want the map to appear.

        $marker = array();     
        $marker['position'] = '42.351437, -71.118556';
        $this->googlemaps->add_marker($marker);

        $data['map'] = $this->googlemaps->create_map();
        // Load our view, passing the map data that has just been created
        
        $this->load->view('view_contact',$data);
    }
    
    
    
    function get_direct() {
        // Load the library
        $this->load->library('googlemaps');
        // Initialize our map. Here you can also pass in additional parameters for customising the map (see below)
         $this->form_validation->set_rules('direct', 'required|trim|max_length[200]|xss_clean');
         
         extract($_POST);
         
        $config['geocodeCaching'] = TRUE;
        $config['adsenseFormat'] = 'MEDIUM_RECTANGLE';
        //$config['center'] = 'auto';
        $config['cluster'] = FALSE;
        $config['clusterGridSize'] = '60';
        $config['directions'] = TRUE;
        $config['directionsStart'] = $direct;//'empire state building';
        $config['directionsEnd'] = '42.351437, -71.118556'; // FIXED TO MET BU HOSPITALITY
        $config['directionsDivID'] = 'directionsDiv';
        $config['draggable'] = FALSE;
        $config['https'] = TRUE;
       // $config['adsenseFormat'] = 'MEDIUM_RECTANGLE';
       // $config['adsenseFormat'] = 'MEDIUM_RECTANGLE';
       
        $config['zoom'] = 'auto';

        $this->googlemaps->initialize($config);
        // Create the map. This will return the Javascript to be included in our pages <head></head> section and the HTML code to be
        // placed where we want the map to appear.
        
        
        $marker = array();     
        $marker['position'] = '42.351437, -71.118556';
        $this->googlemaps->add_marker($marker);

        $data['map'] = $this->googlemaps->create_map();
        // Load our view, passing the map data that has just been created
        
        $this->load->view('view_contact',$data);
                
    }
    
    public function add_issue() {
       
        //check if the user is logged in from the session cockies and if yes will load the member page
        $this->load->model('Model_contact', 'contact');
        // validate the form entered for security
        $this->form_validation->set_rules('body', 'Body', 'required|trim|max_length[200]|xss_clean');
        $this->form_validation->set_rules('title', 'Title', 'required|trim|max_length[50]|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'required|trim|xss_clean|max_length[100]|valid_email|strtolower|callback_email_exists');
       
         if ($this->form_validation->run() == FALSE) {
            // viewing the registration page to the user  
            $this->main();
        } else {
                extract($_POST);
                $post_add['post'] = $this->contact->addIssue($email, $body , $title);

                if (!$post_add['post']) {
                    redirect('/yogette/error', 'location');
                   
                } else {
                    // redirect the user to the main loging page after successful addition
                   
                    redirect('/yogette/contact', 'location');
                   
                }
        }
        
    }
    
    
    function email_exists($email) {
        // this request to set a message for validation for the user in case if the email already exists in DB 
        $this->form_validation->set_message('email_exists', 'this %s email is not registered');
        $this->load->model('Model_contact', 'contact');
        // call the model to check the existanse of the email and if it is there the return will be false
        if ($this->contact->check_exist_email($email)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
    
    function logout()
	{
		/* to destroy the seesion and logout*/
		$this->session->sess_destroy();
                redirect('yogette');
	}
}